<?php

namespace App\Http\Controllers\Strator;

use App\Http\Controllers\Controller;
use App\Services\Strator\StratorService;
use Illuminate\Database\Eloquent\Model;
use App\Http\Requests\Strator\StoreStratorRequest;
use App\Http\Requests\Strator\UpdateStratorRequest;
use App\Models\Strator\Strator;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class StratorController extends Controller
{
    protected Request $request;
    protected Model $model;

    public function __construct(Request $request,Strator $strator)
    {
        $this->request=$request;
        $this->model = $strator;
    }

    /**
     * Display a listing of the resource.
     */
    public function index(StratorService $stratorService):JsonResponse
    {
        $perPage=$this->request->per_page??10;
        $user = $this->request->user();
        if($user->is_super==1){
            // $authIds=$stratorService->getAuthStratorIds($this->request->user());
            $list=$this->model
            ->modelSearch($this->request,$this->model)
            // ->whereIn('id',$authIds)
            ->with(['positions.department','department'])
            ->orderByDesc('id')
            ->paginate($perPage);
        }else{
            // $authIds=$stratorService->getAuthStratorIds($this->request->user());
            $list=$this->model
            ->modelSearch($this->request,$this->model)
            ->where('is_super','!=',1)
            // ->whereIn('id',$authIds)
            ->with(['positions.department','department'])
            ->orderByDesc('id')
            ->paginate($perPage);
        }
        $this->saveOperationLog('查询管理员列表');
        return $this->success($list);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreStratorRequest $request):JsonResponse
    {
        $data=$this->request->except($this->storeExcept());
        $this->model->fill($data);
        if($this->request->filled('strator_password')){
            $this->model->strator_password=Hash::make($this->request->strator_password);
        }
        $this->model->save();
        $this->saveOperationLog('添加管理员');
        return $this->success(['id'=>$this->model->id]);
    }
    //
    public function storeExcept():array
    {
        return [
            'strator_password'
        ];
    }

    /**
     * Display the specified resource.
     */
    public function show()
    {
//        return $this->success($this->request->user());
        $model=$this->model->findOrFail($this->request->id);
        return $this->success($model);
    }

    //
    public function resetPassword()
    {
        $model=$this->model->findOrFail($this->request->id);
        if($model->is_super==1){
            return $this->fail('超级管理员不能重置密码');
        }
        if(empty($this->request->strator_password)){
            return $this->fail('新密码不能为空');
        }
        $model->strator_password=Hash::make($this->request->strator_password);
        $model->save();
        $this->saveOperationLog('修改管理员账号密码');
        return $this->successMsg();
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateStratorRequest $request)
    {
        $model=$this->model->findOrFail($this->request->id);
        $origin=$model->toArray();
        $data=$this->request->except($this->updateExcept());
        $model->fill($data);
        if($this->request->strator_password){
            $model->strator_password=Hash::make($this->request->strator_password);
        }

        $model->save();
        $this->saveOperationLog('修改更新管理员',$origin);
        return $this->successMsg();
    }
    //
    public function updateExcept():array
    {
        return [
            'id',
            'strator_password',
        ];
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy()
    {
        $model=$this->model->findOrFail($this->request->id);
        $model->delete();
        $this->saveOperationLog('删除管理员');
        return $this->successMsg('删除成功');
    }
}
